package com.learning.railwayreservation.dao;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.learning.railwayreservation.vo.BoggieVO;
import com.learning.railwayreservation.vo.TicketVO;

public class TicketDao extends BaseDao{
   public static void main( String args[]){
	TicketDao tdao = new TicketDao();
	List<TicketVO> ticketList =tdao.getTicket(1);
	for (TicketVO ticketVO : ticketList) {
		
		System.out.print(", departure info: " + ticketVO.getDepartureTime());
		System.out.print(", arrival info: " + ticketVO.getArrivalTime());
		System.out.print(", departure from: " + ticketVO.getDepartureFrom());
		System.out.print(", arrival to: " + ticketVO.getArrivalTo());
		System.out.print(", Train Name: " + ticketVO.getTrainName());
		System.out.print(", Train Number: " + ticketVO.getTrainId());
		
		
		
		
	}
   }
	public List<TicketVO> getAllTickets() {

		TicketDao tdao = new TicketDao();
		Connection conn = tdao.getConnection();
		List<TicketVO> ticketList = new ArrayList<TicketVO>();
		TicketVO ticketVO;

		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT * from TICKET");

			while (rs.next()) {
				ticketVO = new TicketVO();
				
				ticketVO.setDepartureTime(rs.getString("DEPARTURE_INFO"));
				ticketVO.setArrivalTime(rs.getString("ARRIVAL_INFO"));
				ticketVO.setDepartureFrom(rs.getString("DEPARTURE_FROM"));
				ticketVO.setArrivalTo(rs.getString("ARRIVAL_TO"));
				
				
				ticketList.add(ticketVO);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ticketList;
	}

	
	public List<TicketVO> getTicket(int pnrNumber) {

		TicketDao tdao = new TicketDao();
		Connection conn = tdao.getConnection();
		List<TicketVO> ticketList = new ArrayList<TicketVO>();
		TicketVO ticketVO;

		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT * from TICKET where PNR =" +pnrNumber);

			while (rs.next()) {
				ticketVO = new TicketVO();
//				ticketVO.setTicketNumber(rs.getInt("TICKET_NUMBER"));
//				ticketVO.setPnrNumber(rs.getString("PNR"));
//				ticketVO.setId(rs.getInt("ID"));
//				ticketVO.setDepartureTime(rs.getString("DEPARTURE_INFO"));
//				ticketVO.setArrivalTime(rs.getString("ARRIVAL_INFO"));
//				ticketVO.setDepartureFrom(rs.getString("DEPARTURE_FROM"));
//				ticketVO.setArrivalTo(rs.getString("ARRIVAL_TO"));
//				ticketVO.setBerth(rs.getString("BERTH"));
//				ticketVO.setCoach(rs.getString("COACH"));
//				
				ticketList.add(ticketVO);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ticketList;
	}


	}

